﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using SqlSugar;
using Seed.StockHandLine.MsDao;
using Seed.StockHandLine.Models;

namespace Seed.StockHandLine.Reposities
{
    public class LongkeCodesResposities
    {
        public bool IsAnyBigCode(string code)
        {
            using (var db = SugarDao.GetInstance())
            {
                return db.Queryable<LongkeCodes>().Any(it => it.BigCode == code);
            }
        }

        public bool IsAnySmlCode(string code)
        {
            using (var db = SugarDao.GetInstance())
            {
                return db.Queryable<LongkeCodes>().Any(it => it.SmlCode == code);
            }
        }

        public void InsertCodes(string code, ListBox smlCodeList, LongkeCodes longke)
        {
            using (var db = SugarDao.GetInstance())
            {
                var lists = (from string sCode in smlCodeList.Items
                    select new LongkeCodes
                    {
                        BigCode = code,
                        SmlCode = sCode,
                        Patch = longke.Patch,
                        ProductName = longke.ProductName,
                        SaleInfo = longke.SaleInfo,
                        BoxNubs = longke.BoxNubs,
                        ProductLine = longke.ProductLine,
                        ProductTime= DateTime.Now,
                        Status="0"
                    }).ToList();
                //db.InsertRange(lists);
                db.SqlBulkCopy(lists);
            }
        }

        public int Counts(string productLine)
        {
            using (var db = SugarDao.GetInstance())
            {
                return
                    //db.SqlQuery<int>(
                    //    "select count(distinct BigCode) from LongkeCodes where datediff(day,ProductTime,getdate())=0 and productline='" +
                    //    productLine + "'").SingleOrDefault();
                db.Queryable<LongkeCodes>().Select("distinct BigCode").Where(it => it.ProductLine == productLine)
                    .Where("datediff(day,ProductTime,getdate())=0").ToList()
                    .Count();
                //db.Queryable<LongkeCodes>().Select("distinct BigCode")
                //    .Count();   
            }
        }

        public LongkeCodes Last(string productLine)
        {
            using (var db = SugarDao.GetInstance())
            {
                return
                    db.Queryable<LongkeCodes>()
                        .Where(it => it.ProductLine == productLine)
                        .Where("datediff(day,ProductTime,getdate())=0")
                        .FirstOrDefault();
            }
        }

        public int LastUpdateInfo(string productLine)
        {
            using (var db = SugarDao.GetInstance())
            {
              LongkeCodes  code=
                    db.Queryable<LongkeCodes>()
                        .Where(it => it.ProductLine == productLine)
                        .OrderBy("Id desc")
                        .FirstOrDefault();
                if (code != null)
                {
                    return Convert.ToInt32(code.Status);
                }
                else
                {
                    return 0;
                }
            }
        }
        public int LastUpdateSaleInfo(string productLine)
        {
            using (var db = SugarDao.GetInstance())
            {
                SaleCodes code =
                      db.Queryable<SaleCodes>()
                          .Where(it => it.ProductLine == productLine)
                          .OrderBy("Id desc")
                          .FirstOrDefault();
                if (code != null)
                {
                    return Convert.ToInt32(code.Status);
                }
                else
                {
                    return 0;
                }
            }
        }


        public void InsertCodes(List<LongkeCodes> list)
        {
            using (var db = SugarDao.GetInstance())
            {
                db.SqlBulkCopy(list);
            }
        }

        public void InsertCodes(List<SaleCodes> list)
        {
            using (var db = SugarDao.GetInstance())
            {
                db.SqlBulkCopy(list);
            }
        }
    }
}
